<?php
/**
 * Author: liqinglin
 * CreateTime: 2019-1-3 9:34
 * description:
 */

namespace app\admin\controller;


use app\admin\model\ServiceErrorMessage;
use app\admin\model\ServiceType;
use think\Db;
use Im\Yunxinapi;
use think\Request;

class Service extends Base
{


    public function index()
    {
        return view('/service/index');
    }

    public function add()
    {
        if (Request::instance()->isGet()) {
            return view('/service/add');
        } else {

        }

    }

    public function Servicelist()
    {
        $s_status = $this->request->param('status', 0);
        $limit = $this->request->param('limit', 10);
        $keyword = $this->request->param('keyword');
        $user_mobile = $this->request->param('user_mobile');
        $user_email = $this->request->param('user_email');
        $search['status'] = $s_status;
        $search['keyword'] = $keyword;
        $search['user_mobile'] = $user_mobile;
        $search['user_email'] = $user_email;
        $dynamicQuery = Db::name('cn_service_base cn_s_b')
            ->join('cn_user_info cn_ui', 'cn_s_b.user_id = cn_ui.user_id', 'inner')
            ->join('cn_service_company cn_s_c', 'cn_s_b.service_id = cn_s_c.service_id', 'left')
            ->order('cn_s_b.create_time', 'desc')
            ->group('cn_s_b.service_id')
            ->join('cn_base_district c_bd', 'cn_s_b.service_province_id=c_bd.district_id', 'left')
            ->join('cn_base_district c_bd_c', 'cn_s_b.service_city_id=c_bd_c.district_id', 'left')
            ->field('cn_s_b.service_id,
            cn_s_b.service_id,
            cn_s_b.service_name,
            cn_s_b.service_object,
            cn_s_b.service_region,
            cn_s_b.service_info_status,
            cn_s_b.create_time,
            cn_s_b.service_province_id,
            cn_s_b.service_city_id,
            cn_s_c.company_name,
            cn_ui.user_name,
            cn_ui.user_mobile,
            cn_ui.user_email,
            cn_s_c.company_name_us,
            c_bd_c.district_name as city,c_bd.district_name as province'
            );
        if ($s_status == 1) {
            $dynamicQuery->whereIn('cn_s_b.service_info_status', [1, 5]);
        } elseif ($s_status == 3) {
            $dynamicQuery->whereIn('cn_s_b.service_info_status', [3, 4]);
        } else {
            $dynamicQuery->where('cn_s_b.service_info_status', $s_status);
        }
        if ($user_mobile) {
            $dynamicQuery->where('cn_ui.user_mobile', $user_mobile);
        }
        if ($user_email) {
            $dynamicQuery->where('cn_ui.user_email', $user_email);
        }
        if (!empty($keyword)) {
            $dynamicQuery = $dynamicQuery->where(" cn_s_c.company_name LIKE '%{$keyword}%' OR  company_name_us LIKE  '%{$keyword}%'");
        }
        $dynamicQuery =$dynamicQuery->order('cn_s_b.user_id desc');
        $data = $dynamicQuery->paginate($limit)->appends($search);
        //var_dump($dynamicQuery->getLastSql());die;
        //var_dump($data);die;
        //var_dump(json_decode(json_encode($data),true));die;

        return view('/service/list', compact('data', 'search'));
    }

    public function ServiceDetail()
    {
        $service_id = $this->request->param('service_id');
        if (empty($service_id)) {
            return json(array('status' => 30000, 'msg' => lang('缺少必要参数')));
        }
        $data = Db::name('cn_service_base cn_s_b')
            ->where('cn_s_b.service_id', $service_id)
            ->join('cn_service_company cn_s_c', 'cn_s_b.service_id = cn_s_c.service_id', 'left')
            ->order('cn_s_b.create_time', 'desc')
            ->field('cn_s_b.service_id,
            cn_s_b.service_object,
            cn_s_b.service_region,
            cn_s_b.create_time,
            cn_s_b.service_province_id,
            cn_s_b.service_city_id,
            cn_s_c.company_name,
            cn_s_c.company_name_us,
            cn_s_c.company_register_province_id,
            cn_s_c.company_register_city_id,
            cn_s_c.company_register_area_id,
            cn_s_c.business_license_location,
            cn_s_c.business_license_location_us,
            cn_s_c.company_province_id,
            cn_s_c.company_city_id,
            cn_s_c.company_area_id,
            cn_s_c.is_three_card,
            cn_s_c.customer_mobile,
            cn_s_c.company_address_detail,
            cn_s_c.company_address_detail_us,
            cn_s_c.contacts_phone,
            cn_s_c.legal_person,
            cn_s_c.company_introduce_en,
            cn_s_c.company_introduce_us,
            cn_s_c.abbreviation,
            cn_s_c.company_phone,
            cn_s_c.company_contacts,
            cn_s_c.company_contacts_us,
            cn_s_c.other_prove,cn_s_c.other_prove_starttime,cn_s_c.other_prove_endtime,cn_s_c.other_prove_islong,   
            cn_s_c.three_card_image,cn_s_c.three_card_starttime,cn_s_c.three_card_endtime,cn_s_c.three_card_islong,
            cn_s_c.bank_account_name,cn_s_c.bank_account_number,cn_s_c.bank_name, 
            cn_s_c.business_license_electronic,cn_s_c.license_electronic_starttime,cn_s_c.license_electronic_endtime,cn_s_c.license_electronic_islong,
            cn_s_c.business_organization_electronic,cn_s_c.organization_electronic_starttime,cn_s_c.organization_electronic_endtime,cn_s_c.organization_electronic_islong,
            cn_s_c.tax_registration ,cn_s_c.tax_registration_starttime, cn_s_c.tax_registration_endtime ,cn_s_c.tax_registration_islong'
            )
            ->find();
        $data['other_prove_starttime'] = substr(str_replace('-', '.', $data['other_prove_starttime']), 0, 7);
        $data['other_prove_endtime'] = substr(str_replace('-', '.', $data['other_prove_endtime']), 0, 7);
        $data['three_card_starttime'] = substr(str_replace('-', '.', $data['three_card_starttime']), 0, 7);
        $data['three_card_endtime'] = substr(str_replace('-', '.', $data['three_card_endtime']), 0, 7);
        $data['license_electronic_starttime'] = substr(str_replace('-', '.', $data['license_electronic_starttime']), 0, 7);
        $data['license_electronic_endtime'] = substr(str_replace('-', '.', $data['license_electronic_endtime']), 0, 7);
        $data['tax_registration_starttime'] = substr(str_replace('-', '.', $data['tax_registration_starttime']), 0, 7);
        $data['tax_registration_endtime'] = substr(str_replace('-', '.', $data['tax_registration_endtime']), 0, 7);
        $data['organization_electronic_endtime'] = substr(str_replace('-', '.', $data['organization_electronic_endtime']), 0, 7);
        $data['organization_electronic_starttime'] = substr(str_replace('-', '.', $data['organization_electronic_starttime']), 0, 7);
        $data['service_types'] = ServiceType::getAllServiceType($service_id);
        $data['service_all_types'] = ServiceType::getAllType();
        return view('/service/detail', compact('data'));
    }

    public function ServiceDetailProve()
    {
        $service_id = $this->request->param('service_id');
        if (empty($service_id)) {
            return json(array('status' => 30000, 'msg' => lang('缺少必要参数')));
        }
        $data = Db::name('cn_service_base cn_s_b')
            ->where('cn_s_b.service_id', $service_id)
            ->join('cn_service_company cn_s_c', 'cn_s_b.service_id = cn_s_c.service_id', 'left')
            ->order('cn_s_b.create_time', 'desc')
            ->field('cn_s_b.service_id,
            cn_s_b.service_object,
            cn_s_b.service_region,
            cn_s_b.create_time,
            cn_s_b.service_province_id,
            cn_s_b.service_city_id,
            cn_s_c.company_name,
            cn_s_c.company_name_us,
            cn_s_c.company_register_province_id,
            cn_s_c.company_register_city_id,
            cn_s_c.company_register_area_id,
            cn_s_c.business_license_location,
            cn_s_c.business_license_location_us,
            cn_s_c.company_province_id,
            cn_s_c.company_city_id,
            cn_s_c.company_area_id,
            cn_s_c.is_three_card,
            cn_s_c.customer_mobile,
            cn_s_c.company_address_detail,
            cn_s_c.company_address_detail_us,
            cn_s_c.contacts_phone,
            cn_s_c.legal_person,
            cn_s_c.company_introduce_en,
            cn_s_c.company_introduce_us,
            cn_s_c.abbreviation,
            cn_s_c.company_phone,
            cn_s_c.company_contacts,
            cn_s_c.company_contacts_us,
            cn_s_c.other_prove,cn_s_c.other_prove_starttime,cn_s_c.other_prove_endtime,cn_s_c.other_prove_islong,   
            cn_s_c.three_card_image,cn_s_c.three_card_starttime,cn_s_c.three_card_endtime,cn_s_c.three_card_islong,
            cn_s_c.bank_account_name,cn_s_c.bank_account_number,cn_s_c.bank_name, 
            cn_s_c.business_license_electronic,cn_s_c.license_electronic_starttime,cn_s_c.license_electronic_endtime,cn_s_c.license_electronic_islong,
            cn_s_c.business_organization_electronic,cn_s_c.organization_electronic_starttime,cn_s_c.organization_electronic_endtime,cn_s_c.organization_electronic_islong,
            cn_s_c.tax_registration ,cn_s_c.tax_registration_starttime, cn_s_c.tax_registration_endtime ,cn_s_c.tax_registration_islong'
            )
            ->find();
        $data['other_prove_starttime'] = substr(str_replace('-', '.', $data['other_prove_starttime']), 0, 7);
        $data['other_prove_endtime'] = substr(str_replace('-', '.', $data['other_prove_endtime']), 0, 7);
        $data['three_card_starttime'] = substr(str_replace('-', '.', $data['three_card_starttime']), 0, 7);
        $data['three_card_endtime'] = substr(str_replace('-', '.', $data['three_card_endtime']), 0, 7);
        $data['license_electronic_starttime'] = substr(str_replace('-', '.', $data['license_electronic_starttime']), 0, 7);
        $data['license_electronic_endtime'] = substr(str_replace('-', '.', $data['license_electronic_endtime']), 0, 7);
        $data['tax_registration_starttime'] = substr(str_replace('-', '.', $data['tax_registration_starttime']), 0, 7);
        $data['tax_registration_endtime'] = substr(str_replace('-', '.', $data['tax_registration_endtime']), 0, 7);
        $data['organization_electronic_endtime'] = substr(str_replace('-', '.', $data['organization_electronic_endtime']), 0, 7);
        $data['organization_electronic_starttime'] = substr(str_replace('-', '.', $data['organization_electronic_starttime']), 0, 7);
        $data['service_types'] = ServiceType::getAllServiceType($service_id);
        return view('/service/detailProve', compact('data'));
    }

    public function editServiceInfo()
    {
        if (Request::instance()->isGet()) {
            $service_id = $this->request->param('service_id');
            $data = Db::name('cn_service_base cn_s_b')
                ->where('cn_s_b.service_id', $service_id)
                ->join('cn_service_company cn_s_c', 'cn_s_b.service_id = cn_s_c.service_id', 'left')
                ->order('cn_s_b.create_time', 'desc')
                ->field('cn_s_b.service_id,
            cn_s_b.service_name,
            cn_s_b.service_name_en,
            cn_s_b.service_object,
            cn_s_b.service_region,
            cn_s_b.create_time,
            cn_s_b.service_province_id,
            cn_s_b.service_city_id,
            cn_s_c.company_name,
            cn_s_c.company_name_us,
            cn_s_c.company_register_province_id,
            cn_s_c.company_register_city_id,
            cn_s_c.company_register_area_id,
            cn_s_c.business_license_location,
            cn_s_c.business_license_location_us,
            cn_s_c.company_province_id,
            cn_s_c.company_city_id,
            cn_s_c.company_area_id,
            cn_s_c.is_three_card,
            cn_s_c.customer_mobile,
            cn_s_c.company_address_detail,
            cn_s_c.company_address_detail_us,
            cn_s_c.contacts_phone,
            cn_s_c.legal_person,
            cn_s_c.company_introduce_en,
            cn_s_c.company_introduce_us,
            cn_s_c.abbreviation,
            cn_s_c.company_phone,
            cn_s_c.company_contacts,
            cn_s_c.company_contacts_us,
            cn_s_c.other_prove,cn_s_c.other_prove_starttime,cn_s_c.other_prove_endtime,cn_s_c.other_prove_islong,   
            cn_s_c.three_card_image,cn_s_c.three_card_starttime,cn_s_c.three_card_endtime,cn_s_c.three_card_islong,
            cn_s_c.bank_account_name,cn_s_c.bank_account_number,cn_s_c.bank_name, 
            cn_s_c.business_license_electronic,cn_s_c.license_electronic_starttime,cn_s_c.license_electronic_endtime,cn_s_c.license_electronic_islong,
            cn_s_c.business_organization_electronic,cn_s_c.organization_electronic_starttime,cn_s_c.organization_electronic_endtime,cn_s_c.organization_electronic_islong,
            cn_s_c.tax_registration ,cn_s_c.tax_registration_starttime, cn_s_c.tax_registration_endtime ,cn_s_c.tax_registration_islong'
                )
                ->find();
            $data['other_prove_starttime'] = substr(str_replace('-', '.', $data['other_prove_starttime']), 0, 7);
            $data['other_prove_endtime'] = substr(str_replace('-', '.', $data['other_prove_endtime']), 0, 7);
            $data['three_card_starttime'] = substr(str_replace('-', '.', $data['three_card_starttime']), 0, 7);
            $data['three_card_endtime'] = substr(str_replace('-', '.', $data['three_card_endtime']), 0, 7);
            $data['license_electronic_starttime'] = substr(str_replace('-', '.', $data['license_electronic_starttime']), 0, 7);
            $data['license_electronic_endtime'] = substr(str_replace('-', '.', $data['license_electronic_endtime']), 0, 7);
            $data['tax_registration_starttime'] = substr(str_replace('-', '.', $data['tax_registration_starttime']), 0, 7);
            $data['tax_registration_endtime'] = substr(str_replace('-', '.', $data['tax_registration_endtime']), 0, 7);
            $data['organization_electronic_endtime'] = substr(str_replace('-', '.', $data['organization_electronic_endtime']), 0, 7);
            $data['organization_electronic_starttime'] = substr(str_replace('-', '.', $data['organization_electronic_starttime']), 0, 7);
            $data['service_types'] = ServiceType::getAllServiceType($service_id);
            $data['service_all_types'] = ServiceType::getAllType();
            $data['service_id'] = $service_id;
            return view('/service/editServiceInfo', compact('data'));
        } elseif (Request::instance()->isPost()) {

            $company_name = $this->request->param('company_name');
            $company_name_us = $this->request->param('company_name_us');
            $service_name = $this->request->param('service_name');
            $service_name_en = $this->request->param('service_name_en');
            $service_id = $this->request->param('service_id');
            $service_type_ids = $this->request->param('service_type_ids');
            if ($service_name) {
                $res = DB::table('cn_service_base')
                    ->where('service_name', $service_name)
                    ->find();
                if ($res && $res['service_id'] != $service_id) {
                    return json(['status' => 40022, 'msg' => lang('服务商名称已存在')]);
                }
                DB::table('cn_service_base')
                    ->where('service_id', $service_id)
                    ->update([
                        'service_name' => $service_name
                    ]);
            }
            if ($service_name_en) {
                $res1 = DB::table('cn_service_base')
                    ->where('service_name_en', $service_name_en)
                    ->find();
                if ($res1 && $res1['service_id'] != $service_id) {
                    return json(['status' => 40022, 'msg' => lang('服务商名称已存在')]);
                }
                DB::table('cn_service_base')
                    ->where('service_id', $service_id)
                    ->update([
                        'service_name_en' => $service_name_en
                    ]);
            }
            if ($company_name && $company_name_us) {
                DB::table('cn_service_company')
                    ->where('service_id', $service_id)
                    ->update([
                        'company_name' => $company_name,
                        'company_name_us' => $company_name_us
                    ]);
            }
            if ($service_type_ids) {
                $service_type_ids = json_decode($service_type_ids);
                ServiceType::saveServiceType($service_id, $service_type_ids);
            }
            return json(array('status' => 200, 'msg' => lang('成功'), 'data' => ''));
        }

    }


    public function ExamineServiceRefuse(\think\Request $request)
    {
        return view('/service/serviceRefuse', ['service_id' => input('service_id')]);
    }


    /**
     * @api {post} /admin/Service/ExamineService 审核服务商
     * @apiVersion 1.0.0
     * @apiGroup Service
     * @apiPermission ALL
     *
     * @apiDescription 审核服务商
     * @apiHeader  {string} token
     * @apiParam {string} [request_type] 请求来源('h5','android','ios','pc')
     * @apiParam {int} service_info_status 审核状态 (1、审核通过，2、已拒绝)
     * @apiParam {int} service_id 服务商id
     * @apiParam {int} [audit_msg] 拒绝原因 ,拒绝时必填
     * @apiParam {int} [refuse_step] 拒绝步骤数(2,3),拒绝时必填
     *
     * @apiSuccessExample {json} 成功示例:
     *{"status":200,"msg":"成功","data":[]}
     */
    public function ExamineService(\think\Request $request)
    {
        $service_id = $this->request->param('service_id');
        $service_info_status = $this->request->param('service_info_status');
        $postdata = $request->only('error_message');
        $error_message = isset($postdata['error_message']) ? $postdata['error_message'] : '';
        $refuse_step = $this->request->param('refuse_step');
        if (empty($service_id) || empty($service_info_status)) {
            return json(array('status' => 30000, 'msg' => lang('缺少必要参数')));
        }
        if ($service_info_status == 2 && (empty($error_message) || empty($refuse_step))) {
            return json(array('status' => 30000, 'msg' => lang('缺少必要参数')));
        }
        if (!in_array($service_info_status, [2, 1])) {
            return json(array('status' => 30002, 'msg' => lang('参数不是指定的范围')));
        }
        if ($service_info_status == 2 && !in_array($refuse_step, [2, 3])) {
            return json(array('status' => 30002, 'msg' => lang('参数不是指定的范围')));
        }
        $service_info_status = ($service_info_status == 1) ? 5 : 2;
        $savedata = [
            'service_info_status' => $service_info_status,
            'refuse_step' => $refuse_step,
            'is_news' => 1,
        ];
        $res = Db::name('cn_service_base')
            ->where('service_id', $service_id)
            ->find();
        if (empty($res)) {
            return json(array('status' => 30007, 'msg' => lang('不存在该服务商')));
        }
        Db::name('cn_service_base')
            ->where('service_id', $service_id)
            ->update($savedata);
        if ($service_info_status == 2) {
            ServiceErrorMessage::saveErrorMessage($error_message, $service_id);
        } else {
            $service_info = Db::name('cn_service_base c_sb')
                ->where('c_sb.service_id', $service_id)
                ->join('cn_service_company c_sc', 'c_sc.service_id=c_sb.service_id', 'left')
                ->field('c_sb.user_id ,c_sc.company_name')
                ->find();
            Db::name('cn_user_info')
                ->where('user_id', $service_info['user_id'])
                ->update(['user_nickname' => $service_info['company_name']]);
            $user_info = Db::table('cn_user_info')
                ->where('user_id', $service_info['user_id'])
                ->find();
            (new Yunxinapi())->updateUinfo($user_info['user_name'], $user_info['user_nickname'], $user_info['user_logo']);
            ServiceErrorMessage::delErrorMessage($service_id);
        }
        return json(array('status' => 200, 'msg' => lang('成功'), 'data' => ''));
    }


    /**
     * @api {post} /admin/Service/getOneServiceDetail 获取服务商的详细信息
     * @apiVersion 1.0.0
     * @apiGroup Service
     * @apiPermission ALL
     *
     * @apiDescription 审核服务商
     * @apiHeader  {string} token
     * @apiParam {string} [request_type] 请求来源('h5','android','ios','pc')
     * @apiParam {int} status 审核状态 (0、待审核，1、审核通过，2、已拒绝)
     *
     * @apiSuccessExample {json} 成功示例:
     *{"status":200,"msg":"成功","data":{"service_id":"S_154607049308133886","service_object":1,"service_region":1,"create_time":0,"service_province_id":3,"service_city_id":74,"company_name":"333355","company_name_us":"浙江55533","company_register_province_id":1,"company_register_city_id":1,"company_register_area_id":1,"business_license_location":"浙江555333444","company_province_id":1,"company_city_id":1,"company_area_id":1,"company_address_detail":"撒飒飒","company_address_detail_us":"","contacts_phone":"1768232699","legal_person":"还好","company_introduce_en":"测试","company_introduce_us":"测试","abbreviation":"家居基金军军","company_phone":"12212121","company_contacts":"33333","company_contacts_us":"4444","is_three_card":0,"other_prove":"","other_prove_starttime":"","other_prove_endtime":"","other_prove_islong":0,"three_card_image":"","three_card_starttime":"","three_card_endtime":"","three_card_islong":0,"bank_account_name":"","bank_account_number":"","bank_name":"","business_license_electronic":"","license_electronic_starttime":"","license_electronic_endtime":"","license_electronic_islong":0,"business_organization_electronic":"","organization_electronic_starttime":"","organization_electronic_endtime":"","tax_registration":"","tax_registration_starttime":"","tax_registration_endtime":"","tax_registration_islong":0}}
     * @apiSuccess {int} service_object  服务对象 1-进口商/2-出口商/3-其他
     * @apiSuccess {int} service_region 服务区域 1国内,2国外
     * @apiSuccess {int} service_province_id 省ID(国内)
     * @apiSuccess {int} service_city_id 城市ID(国内)
     *
     * @apiSuccess {string} company_name  公司名称
     * @apiSuccess {string} company_name_us  公司英文名称
     * @apiSuccess {int} company_register_province_id 公司注册省份ID
     * @apiSuccess {int} company_register_city_id  公司注册市区城市ID
     * @apiSuccess {int} company_register_area_id 公司注册区域
     * @apiSuccess {string} business_license_location 注册地的详细地址
     * @apiSuccess {string} company_province_id 公司地址省ID
     * @apiSuccess {int} company_city_id 公司地址城市ID
     * @apiSuccess {int} company_area_id 公司地址地区ID
     * @apiSuccess {string} company_address_detail 公司地址详细信息
     * @apiSuccess {string} company_address_detail_us 公司地址(英文)
     * @apiSuccess {string} contacts_phone 公司联系人电话
     * @apiSuccess {string} legal_person 法定代表人姓名
     * @apiSuccess {string} company_introduce_en 公司介绍(中文)
     * @apiSuccess {string} company_introduce_us 公司介绍(英文)
     * @apiSuccess {string} abbreviation 公司简称
     * @apiSuccess {string} company_phone 公司电话
     * @apiSuccess {string} company_contacts 公司联系人
     * @apiSuccess {string} company_contacts_us 公司联系人(英文)
     * @apiSuccess {string} customer_mobile 客服电话
     *
     * @apiSuccess {int} is_three_card  是否为三证合一(0否,1是)
     * @apiSuccess {string} other_prove] 其他证明图片url
     * @apiSuccess {string} other_prove_starttime  其他证明开始时间 格式: 2018-05-10
     * @apiSuccess {string} other_prove_endtime  其他证明结束时间 格式: 2028-05-10
     * @apiSuccess {int} [other_prove_islong  其他证明是否长期有效(0,非长期有效,1长期有效)
     *
     * @apiSuccess {string} three_card_image  三证合一图片url (当 is_three_card =0 时 可选字段)
     * @apiSuccess {string} three_card_starttime  三证合一开始时间 格式: 2018-05-10 (当 is_three_card =0 时 可选字段)
     * @apiSuccess {string} three_card_endtime  三证合一结束时间 格式: 2018-05-10 (当 is_three_card =0 时 可选字段)
     * @apiSuccess {int} three_card_islong  三证合一是否长期有效(0,非长期有效,1长期有效) (当 is_three_card =0 时 可选字段)
     *
     * @apiSuccess {string} bank_account_name  开户账号名称
     * @apiSuccess {string} bank_account_number  开户银行账号
     * @apiSuccess {string} bank_name  开户银行
     *
     * @apiSuccess {string}  business_license_electronic 营业执照电子版 (当 is_three_card 字段=1 时 可选字段)
     * @apiSuccess {string}  license_electronic_starttime 营业执照开始日期 格式: 2018-05-10 (当 is_three_card =1 时 可选字段)
     * @apiSuccess {string}  license_electronic_endtime 营业执照结束日期  格式: 2018-05-10 (当 is_three_card =1 时 可选字段)
     * @apiSuccess {int}  license_electronic_islong 营业执照(0,非长期有效,1长期有效) (当 is_three_card =1 时 可选字段)
     *
     * @apiSuccess {string}  business_organization_electronic 组织机构代码证图片url (当 is_three_card =1 时 可选字段)
     * @apiSuccess {string}  organization_electronic_starttime 组织代码有效期开始时间 格式: 2018-05-10 (当 is_three_card =1 时 可选字段)
     * @apiSuccess {string}  organization_electronic_endtime 组织代码证有效期结束时间 格式: 2018-05-10 (当 is_three_card =1 时 可选字段)
     * @apiSuccess {int}  organization_electronic_islong 组织代码证(0,非长期有效,1长期有效) (当 is_three_card =1 时 可选字段)
     * @apiSuccess {string}  tax_registration 税务登记证图片url (当 is_three_card =1 时 可选字段)
     * @apiSuccess {string}  tax_registration_starttime 税务登记有效期开始时间 格式: 2018-05-10 (当 is_three_card =1 时 可选字段)
     * @apiSuccess {string}  tax_registration_endtime  税务登记有效期结束时间  格式: 2018-05-10 (当 is_three_card =1 时 可选字段)
     * @apiSuccess {int}  tax_registration_islong 税务登记(0,非长期有效,1长期有效)  (当 is_three_card =1 时 可选字段)
     *
     */
    public function getOneServiceDetail()
    {
        $service_id = $this->request->param('service_id');
        if (empty($service_id)) {
            return json(array('status' => 30000, 'msg' => lang('缺少必要参数')));
        }
        $data = Db::name('cn_service_base cn_s_b')
            ->where('cn_s_b.service_id', $service_id)
            ->join('cn_service_company cn_s_c', 'cn_s_b.service_id = cn_s_c.service_id', 'left')
            ->order('cn_s_b.create_time', 'desc')
            ->field('cn_s_b.service_id,
            cn_s_b.service_object,
            cn_s_b.service_region,
            cn_s_b.create_time,
            cn_s_b.service_province_id,
            cn_s_b.service_city_id,
            cn_s_c.company_name,
            cn_s_c.company_name_us,
            cn_s_c.company_register_province_id,
            cn_s_c.company_register_city_id,
            cn_s_c.company_register_area_id,
            cn_s_c.business_license_location,
            cn_s_c.business_license_location_us,
            cn_s_c.company_province_id,
            cn_s_c.company_city_id,
            cn_s_c.company_area_id,
            cn_s_c.customer_mobile,
            cn_s_c.company_address_detail,
            cn_s_c.company_address_detail_us,
            cn_s_c.contacts_phone,
            cn_s_c.legal_person,
            cn_s_c.company_introduce_en,
            cn_s_c.company_introduce_us,
            cn_s_c.abbreviation,
            cn_s_c.company_phone,
            cn_s_c.company_contacts,
            cn_s_c.company_contacts_us,
            cn_s_c.is_three_card,
            cn_s_c.other_prove,cn_s_c.other_prove_starttime,cn_s_c.other_prove_endtime,cn_s_c.other_prove_islong,   
            cn_s_c.three_card_image,cn_s_c.three_card_starttime,cn_s_c.three_card_endtime,cn_s_c.three_card_islong,
            cn_s_c.bank_account_name,cn_s_c.bank_account_number,cn_s_c.bank_name, 
            cn_s_c.business_license_electronic,cn_s_c.license_electronic_starttime,cn_s_c.license_electronic_endtime,cn_s_c.license_electronic_islong,
            cn_s_c.business_organization_electronic,cn_s_c.organization_electronic_starttime,cn_s_c.organization_electronic_endtime,cn_s_c.organization_electronic_islong,
            cn_s_c.tax_registration ,cn_s_c.tax_registration_starttime, cn_s_c.tax_registration_endtime ,cn_s_c.tax_registration_islong'
            )
            ->find();
        $data['other_prove_starttime'] = substr(str_replace('-', '.', $data['other_prove_starttime']), 0, 7);
        $data['is_three_card'] = (int)$data['is_three_card'];
        $data['other_prove_endtime'] = substr(str_replace('-', '.', $data['other_prove_endtime']), 0, 7);
        $data['three_card_starttime'] = substr(str_replace('-', '.', $data['three_card_starttime']), 0, 7);
        $data['three_card_endtime'] = substr(str_replace('-', '.', $data['three_card_endtime']), 0, 7);
        $data['license_electronic_starttime'] = substr(str_replace('-', '.', $data['license_electronic_starttime']), 0, 7);
        $data['license_electronic_endtime'] = substr(str_replace('-', '.', $data['license_electronic_endtime']), 0, 7);
        $data['tax_registration_starttime'] = substr(str_replace('-', '.', $data['tax_registration_starttime']), 0, 7);
        $data['tax_registration_endtime'] = substr(str_replace('-', '.', $data['tax_registration_endtime']), 0, 7);
        $data['organization_electronic_endtime'] = substr(str_replace('-', '.', $data['organization_electronic_endtime']), 0, 7);
        $data['organization_electronic_starttime'] = substr(str_replace('-', '.', $data['organization_electronic_starttime']), 0, 7);
        $data['service_types'] = ServiceType::getAllServiceType($service_id);

        return json(array('status' => 200, 'msg' => lang('成功'), 'data' => $data));
    }


    /**
     * @api {post} /admin/Service/registered 服务商后台给用户注册
     * @apiVersion 1.0.0
     * @apiGroup Service
     * @apiPermission ALL
     *
     * @apiDescription 服务商后台给用户注册
     * @apiParam {string} [request_type] 请求来源('h5','android','ios','pc')
     * @apiParam {string} account  账号 (邮箱<br>15988491991@qq.com 或者 手机号  ) base64加密
     * @apiParam {string} password 密码<br>(用户填写的记得md5加密后传输)
     * @apiParam {string} repassword 重复密码<br>(用户填写的记得md5加密后传输)
     * @apiParam {int} [countrycode] 国家代码
     *
     * @apiSuccessExample {json} 成功示例:
     * {"status":200,"msg":"注册成功","data":{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjbnNlbGxlcnMiLCJpYXQiOjE1NDU2NDA5MjUsImV4cCI6MTU0NTY0ODEyNSwidXNlcl9pZCI6MTU0NTY0MDkyNSwic2hvcF9pZCI6MH0._Y3pC9UoEAVY3OXeO9AU4nET_A_K0SS-zxPt2rNVfU4","token_create_time":"2018-12-24 16:42:05","user_logintime":"2018-12-24 16:42:05","token_expire_time":1545648125}}
     *
     * @apiSuccess (响应数据) {string} token 令牌
     * @apiSuccess (响应数据) {datetime} user_logintime 用户登录时间
     * @apiSuccess (响应数据) {string} token_create_time token生成时间
     * @apiSuccess (响应数据) {string} token_expire_time token过期时间
     */
    public function registered()
    {
        $password = $this->request->param('password');     // '密码';
        $repassword = $this->request->param('repassword');     // '重复密码';
        $countrycode = $this->request->param('countrycode/d', 86);
        $account = $this->request->param('account', '');
        if (empty($account)) {
            return json(['status' => 20001, 'msg' => '请输入登录帐号']);
        }
        //$account = base64_decode($account);
        $register_type = (strpos($account, '@') !== false) ? 11 : 12;

        //进行验证
        if ($register_type == 12 && (($countrycode == 86 && preg_match('/^(((13[0-9])|(14[579])|(15([0-3]|[5-9]))|(16[6])|(17[0135678])|(18[0-9])|(19[89]))\\d{8})$/', $account) == false) || ($countrycode != 86 && preg_match('\(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$', $account) == false))) {
            return json(array('status' => 10014, 'msg' => lang('请填写正确的手机号地址')));
        }
        if ($register_type == 11 && preg_match('/^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$/', $account) == false) {
            return json(array('status' => 10014, 'msg' => lang('请填写正确的邮箱地址')));
        }
        $user_type = 3;//1卖家，2买家，3服务商

        if ($password != $repassword) {
            return json(array('status' => 406, 'msg' => lang('两次输入密码不一致')));
        }
        $password = md5($password);
        //自定义一些数据
        $user_id = time();
        $user_ip = $_SERVER['REMOTE_ADDR'];
        $environment_prefix_user_name = $user_id;
        $bind_id = ($register_type == 12) ? 'mobile_' . $countrycode . '_' . $account : 'email_' . strtolower($account);
        //查询是否已注册
        if ($register_type == 12) {
            $bind_result = DB::name('ucenter_user_bind_connect')
                ->where(" bind_id=:mobile_bind_id and bind_type=:mobile_bind_type ")
                ->bind(['mobile_bind_id' => $bind_id, 'mobile_bind_type' => 12])
                ->find();
        } elseif ($register_type == 11) {
            $account = strtolower($account);
            $bind_result = DB::name('ucenter_user_bind_connect')
                ->where(" bind_id=:email_bind_id and bind_type=:email_bind_type ")
                ->bind(['email_bind_id' => $bind_id, 'email_bind_type' => 11])
                ->find();
        }
        if (empty($bind_result)) {
            //开启事务
            DB::startTrans();
            try {
                //ucenter库 --- start
                $ucenter_user_bind_connect_data = array(
                    'bind_id' => $bind_id,
                    'bind_type' => $register_type,
                    'user_id' => $user_id,
                    'bind_time' => date("Y-m-d H:i", $user_id)
                );
                DB::table('ucenter_user_bind_connect')->insert($ucenter_user_bind_connect_data);
                $ucenter_user_info_detail_data = array(
                    'user_name' => config('environment_prefix_user_name') . $user_id,
                    'user_mobile_county_code' => $countrycode,
                    'user_mobile' => $register_type == 12 ? $account : '',
                    'user_email' => $register_type == 11 ? strtolower($account) : '',
                    'user_mobile_verify' => $register_type == 12 ? 1 : 0,
                    'user_email_verify' => $register_type == 11 ? 1 : 0
                );
                DB::table('ucenter_user_info_detail')->insert($ucenter_user_info_detail_data);

                $ucenter_user_info_data = array(
                    'user_id' => $user_id,
                    'user_name' => config('environment_prefix_user_name') . $user_id,
                    'password' => $password,
                    'action_time' => $user_id,
                    'action_ip' => $user_ip,
                    'session_id' => uniqid(),
                    'user_type' => $user_type,
                    'user_country_type' => ($register_type == 12) ? 1 : 2
                );
                Db::table('ucenter_user_info')->insert($ucenter_user_info_data);
                //ucenter库 --- end


                //shop库 --- start
                $cn_user_info_data = array(
                    'user_id' => $user_id,
                    'user_nickname' => $environment_prefix_user_name,
                    'user_mobile' => $register_type == 12 ? $account : '',
                    'user_mobile_county_code' => $countrycode,
                    'user_email' => $register_type == 11 ? strtolower($account) : '',
                    'user_name' => config('environment_prefix_user_name') . $user_id,
                    'user_regtime' => date("Y-m-d H:i", time()),
                    'user_role' => 1
                );
                Db::table('cn_user_info')->insert($cn_user_info_data);
                $cn_user_base_data = array(
                    'user_id' => $user_id,
                    'user_account' => config('environment_prefix_user_name') . $user_id,
                    'user_key' => 'ttt',
                    'user_login_times' => 1,
                    'user_login_time' => date("Y-m-d H:i", time()),
                    'user_login_ip' => $user_ip,
                    'user_type' => $user_type,
                    'seller_type' => 0
                );
                Db::table('cn_user_base')->insert($cn_user_base_data);
                $service_id = getUniqueId('s_');
                Db::table('cn_service_base')->insert(['user_id' => $user_id, 'service_id' => $service_id]);
                Db::table('cn_service_company')->insert(['service_id' => $service_id]);
                //shop库 --- end
                Db::commit();
                //注册云信账号
                $Yx = new Yunxinapi();
                $result = $Yx->createUserId($cn_user_info_data['user_name'], $cn_user_info_data['user_nickname'], '');
                DB::name('cn_user_info')->where('user_id', $user_id)->update(['im_user_token' => $result['info']['token']]);

                return json(array('status' => 200, 'msg' => lang('注册成功'), 'data' => ''));
            } catch (\Exception $e) {
                Db::rollback();
                throw $e;
            }

        } else {
            return json(array('status' => 201, 'msg' => lang('该用户已经存在,不能进行注册')));
            $res = Db::table('ucenter_user_info')->where('user_id', $bind_result['user_id'])->find();
            if ($res['user_type'] != $user_type) {
                //更新用户的类型
                Db::table('ucenter_user_info')->where('user_id', $bind_result['user_id'])->update(['user_type' => $user_type]);
                $service_id = getUniqueId('s_');
                Db::table('cn_service_base')->insert(['user_id' => $bind_result['user_id'], 'service_id' => $service_id]);
                Db::table('cn_service_company')->insert(['service_id' => $service_id]);
            }
            return json(array('status' => 200, 'msg' => lang('升级用户类型成功')));
        }


    }

    public function resetPassword()
    {
        $service_id = $this->request->param('service_id');
        if (empty($service_id)) {
            return json(array('status' => 30000, 'msg' => lang('缺少必要参数')));
        }
        $service_info = DB::table('cn_service_base')->where('service_id', $service_id)
            ->find();
        if ($service_info) {
            DB::table('ucenter_user_info')->where('user_id', $service_info['user_id'])
                ->update(['password' => md5('123456')]);
        }
        return json(array('status' => 200, 'msg' => lang('成功')));
    }

    public function del()
    {
        $service_id = $this->request->param('service_id');
        if (empty($service_id)) {
            return json(array('status' => 30000, 'msg' => lang('缺少必要参数')));
        }
        $service_info = DB::table('cn_service_base')->where('service_id', $service_id)
            ->find();
        if ($service_info) {
            DB::table('cn_service_base')->where('service_id', $service_id)
                ->update(['service_info_status' => 6]);
        }
        return json(array('status' => 200, 'msg' => lang('成功')));
    }

}